﻿// ----------------------------------------
// 2020-10-6 
// wng0@sina.com
// 弓箭类
// ---------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Arrow : MonoBehaviour
{
    float g;      
    //float distance;
    float zSpeed;
    float ySpeed;
    float rSpeed;
    float startAngle;
    float flyTime;
    Vector3 currentLocalPosition;
    Vector3 currentAngle;
    bool fire=false;
    public RoleCatagory targetTag;
    void Start()
    {
        targetTag=RoleCatagory.None;
    }
    public void SetupArrow(float distance, RoleCatagory tTag)
    { 
        startAngle=-45;
        flyTime=2;         
        zSpeed=distance/flyTime;
        ySpeed=zSpeed;
        g=zSpeed;   
        currentLocalPosition=this.transform.localPosition;  
        currentAngle=this.transform.eulerAngles; 
        currentAngle.x=startAngle;
        //this.transform.eulerAngles=currentAngle;         
        rSpeed=90/flyTime;
        targetTag=tTag;
        Debug.Log(this.gameObject.name+targetTag);
        fire=true;
    }
    void Update()
    {
        if (fire==true)
        {
            CalculatePosition();
        }
    }
    void CalculatePosition()
    {
        currentLocalPosition+=transform.forward*zSpeed*Time.deltaTime;        
        //this.transform.Translate(Vector3.forward*zSpeed*Time.deltaTime);
        ySpeed-=g*Time.deltaTime;
        currentLocalPosition.y+=ySpeed*Time.deltaTime;
        currentAngle.x+=rSpeed*Time.deltaTime;
        this.transform.localPosition=currentLocalPosition;
        this.transform.eulerAngles=currentAngle;
    }
    void OnTriggerEnter( Collider other)
    {
        if (other.tag=="Ground")
        {
            fire=false;
            //targetTag=null;
            PoolManager.Instance.Despawn(this.gameObject);
        }
        else if (other.tag=="Role")
        {
            if(other.GetComponent<Role>().m_roleCatagory==targetTag)
            {
                fire=false;
                //targetTag=null;
                PoolManager.Instance.Despawn(this.gameObject);
            }
        }
    }
}
